/**
 * Created by kevin on 2017/08/04.
 *
 * 抽奖弹窗
 *"id": "popuplayer_1_close_bn",
 *"src": "imgs/close.png?v="+version,
 *"group_id": "popuplayer_1",
 *"prop":{"scale":比率,"offset":{"x":比率,"y":比率},"align":"BM"},比率＝屏幕尺寸的
 */


var eosi = window.eosi || {};
this.eosi.website = this.eosi.website || {};

(function (window) {
    // the application


    function PopupLottery(config,loader) {

        this.Container_constructor();
        this.config = config;
        this.loader = loader;
        this.init();
        //console.log(this.father.targetWidth);

    }
    //PopupLayer.TYPE = "people";


    var p = createjs.extend(PopupLottery, createjs.Container);

    p.className = "PopupLottery";

    //配置
    p.config;
    p.loader;
    //背景遮罩
    p.bg;


    // public methods:
    p.init = function () {
        //console.log(this.parent)
        //return;
        this.drawPopup();
        //console.log(this.className);
    }

    p.onClick = function(evt){
        this.parent.removeChild(this);
        //console.log(this.parent.removeChild(this))
    }

    /*
     * 绘制popup
     * */
    p.drawPopup = function(){


        for(var i = 0 ;i<this.config.length;i++){
            if(this.config[i].id == this.config[i].group_id+"_bg"){
                this.bg = new createjs.Container();
                var bgC = new createjs.Shape();
                bgC.graphics.beginFill(this.config[i].bg.color).drawRect(0,0,targetWidth,targetHeight);
                bgC.alpha = this.config[i].bg.alpha;
                this.bg.addChild(bgC);
                this.addChild(this.bg)
            }


            var pic = this.loader.getResult(this.config[i].id);
            var item;
            //console.log(this.resItems[i].r_type.type)
            switch(this.config[i].r_type.type){
                case "bitmap":
                    item = new createjs.Bitmap(pic);
                    break;
                case "spritesheet":
                    var draw_content=this.resItems[i].prop["draw"];//精灵绘制配置对象
                    item = new eosi.website.BaseSpriteSheet(pic,draw_content);
                    break;
                case "baseslide":
                    item = new eosi.website.BaseSlide(pic);
                    break;
                case "button":
                    var bitmap = new createjs.Bitmap(pic);
                    item = new eosi.website.BaseButton(bitmap);
                    //item.addChild(bitmap);
                    break;
                case "class":
                    var temClass = eval(this.resItems[i].r_type.className);
                    item = new temClass(this.resItems[i]);
                    //console.log(this.loader.getResult(this.resItems[i].id))
                    //item = new temClass(this.loader.getResult(this.resItems[i].id));
                    //console.log(this.resItems[i].r_type)
                    break;
                default :
                    item = new createjs.Bitmap(pic);
                    break;
            }

            item.name = this.config[i].id;
            item.resIndex = i;
            this.addChild(item);
            //console.log(item.name)
            if(item.isDraw != undefined && item.isDraw != true){
                console.log(item,"has not be drawed")
                return;
            }else{
                this.setScale(item,this.config[i]);
                this.setPos(item,this.config[i]);
                //animation
                this.setAnimation(item,this.config[i]);
                this.setAction(item,this.config[i]);
            }

            if(this.config[i].id == this.config[i].group_id+"_close_bn"){
                item.on("click",this.onClick,this);
            }
            //action
        }
        this.isDraw = true;

    }


    /*
     * 设置scale
     * */
    p.setScale = function(item,config){
        var pic = item;
        // this.loader.getResult(this.resItems[i].id);
        switch (config.prop.fit){
            case "fit_all"://强制全屏
                //item.setTransform(0,0,targetWidth/pic.getBounds().width,targetHeight/pic.getBounds().height);
                item.scaleX= targetWidth/pic.getBounds().width
                item.scaleY = targetHeight/pic.getBounds().height
                //console.log(item.scaleX)
                break;
            case "fit_screen"://根据屏幕比例适配，不变性(相对于屏幕的比例)
                item.regX = pic.getBounds().width*.5;
                item.regY = pic.getBounds().height*.5;
                var ratio = config.prop.scale*targetWidth/pic.getBounds().width;
                //item.setTransform(0,0,ratio,ratio);
                item.scaleX=item.scaleY= ratio;

                if(item.oriScale){
                    item.oriScale = ratio
                }
                break;
            default :
                break;
        }
    }

    /*
     * 设置位置
     * */
    p.setPos = function(item,config){
        if(config.pos.type == "abs"){//绝对位置
            switch (config.prop.align){
                case "TL":
                    item.x = 0;
                    item.y = 0;
                    break;
                case "TR":
                    item.x = targetWidth;
                    item.y = 0;
                    break;
                case "TM":
                    item.x = targetWidth*0.5;
                    item.y = 0;
                    break;
                case "BL":
                    item.x = 0;
                    item.y = targetHeight;
                    break;
                case "BR":
                    item.x = targetWidth;
                    item.y = targetHeight;
                    break;
                case "BM":
                    item.x = targetWidth*.5;
                    item.y = targetHeight;
                    break;
                case "MM":
                    item.x = targetWidth*.5;
                    item.y = targetHeight*.5;
                    break;
            }
        }else if(config.pos.type == "rel"){//相对位置
            var father = this.getChildByName(config.pos.father);
            //console.log(father)
            if(father){
                item.x = father.x;
                item.y = father.y;
                //如果父亲也有动画，使用目标值作为参数的zhi
                if(father.proObj!=undefined){
                    //console.log(father.proObj)
                    if(father.proObj.y !=undefined){
                        item.y = father.proObj.y;
                    }
                    if(father.proObj.x !=undefined){
                        item.y = father.proObj.x;
                    }

                }

            }

        }

        item.x+=config.prop.offset.x * targetWidth;
        item.y+=config.prop.offset.y * targetHeight;
    }

    /*
     * 设置action
     * */
    p.setAction = function(item,config){
        var action = config.action
        if( action != "none"){
            var hit = new createjs.Shape();
            hit.graphics.beginFill("#000").drawRect(0,0,item.getBounds().width,item.getBounds().height);  // opaque black 50x50 square
            item.hitArea = hit;
            item.on("click",this.onClickItem,this);
            switch (action.type){
                case "popup"://弹窗s
                    item.group_id = action.group_id;
                    break;
                case "jumpto"://跳转
                    item.page = action.page;
                    break;
            }
            //window.location = "http://mywebsite.com/choose.php";

        }
    }

    /*
     * 设置animation
     * */
    p.setAnimation = function(item,config){
        var config = config.tween;
        //console.log(this.name);
        if(config){
            var tween = createjs.Tween.get(item,{loop:config.loop});
            if(config.wait){
                tween.wait(config.wait);
            }
            item.proObj = {};//记住对象的最终属性
            for(var key in config.prop){
                item.proObj[key] = item[key];//将目标参数记录在obj 中
                if(key!="y" && key!="x"){
                    item[key] = config.prop[key];//改变初始参数
                }else{
                    if(key=="y"){
                        item[key] = config.prop[key]*targetHeight;
                    }else{
                        item[key] = config.prop[key]*targetWidth;
                    }

                }

            }
            //console.log(item.proObj);
            tween.to(item.proObj, config.during,createjs.Ease[config.ease]);
            if(config.loop){
                tween.to(config.prop, config.during,createjs.Ease[config.ease]);
            }
            if(config.call){
                try
                {
                    tween.call(eval(config.call));
                }catch(e){
                    console.log(this.className+" no func '"+config.call+"'");
                    //alert("aa");

                }
            }

        }
    }

    /*
     * 点击事件
     * */
    p.onClickItem = function(evt){
        if(evt.target.page){
            //console.log(evt.target.page)
            //window.location = "/#/"+evt.target.page;
            contentLayer.jumpTo(evt.target.page);
        }
        if(evt.target.group_id){
            popupLayer.createPopup(evt.target.group_id);
        }
    }




    // add MyApp to myNameSpace
    window.eosi.website.PopupLottery = createjs.promote(PopupLottery, "Container");
//}());
}(window));





